package com.nnxy.work_to_study.controller;


import com.nnxy.work_to_study.entity.Job;
import com.nnxy.work_to_study.service.JobService;
import com.nnxy.work_to_study.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author @easily001
 * @since 2021-09-29
 */
@RestController
@RequestMapping("/work_to_study/job")
public class JobController {

    @Autowired
    private JobService jobService;

    @RequestMapping("/addJob")
    @ResponseBody
    public ResultUtil addProductDict(@RequestBody Map map) {
       Job job=new Job();  //创建job对象

       String id=String.valueOf(map.get("id"));


       job.setName(String.valueOf(map.get("jobName")));
       job.setProjectId(Integer.valueOf(String.valueOf(map.get("aloneProject"))));
       job.setAddress(String.valueOf(map.get("jobAddr")));
       job.setSalary(new BigDecimal(String.valueOf(map.get("jobSal"))));
       job.setRequirement(String.valueOf(map.get("requirement")));
       job.setSex(String.valueOf(map.get("sex")));
       job.setWorkTime(String.valueOf(map.get("workTime")));

        if(id!=null && !"".equals(id) && !"null".equals(id)){
            job.setId(Integer.valueOf(id));
            jobService.updateJob(job);
        }else{
            jobService.addJob(job);
        }
        return ResultUtil.success("操作成功!");
    }

    //查询
    @RequestMapping(value = "getJobList",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public ResultUtil getJobList(@RequestBody Map map){
        String name = String.valueOf(map.get("name"));
        String projectId = String.valueOf(map.get("projectId"));

        Integer page=Integer.valueOf(String.valueOf(map.get("page")));
        Integer limit=Integer.valueOf(String.valueOf(map.get("limit")));
        return jobService.getJobList(name,projectId,page,limit);
    }


    @RequestMapping("/deleteJob")
    @ResponseBody
    public ResultUtil deleteJob(@RequestBody Map map) {
        int num = jobService.deleteJob(Integer.valueOf(String.valueOf(map.get("id"))));
        if(num > 0){
            return ResultUtil.success("删除成功!");
        }else{
            return ResultUtil.error("删除失败!");
        }
    }


    @RequestMapping(value = "updateJob",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public ResultUtil updateJob(@RequestBody Map map) {

        Job job=new Job();
        job.setId(Integer.valueOf(String.valueOf(map.get("id"))));
        job.setName(String.valueOf(map.get("jobName")));
        job.setProjectId(Integer.valueOf(String.valueOf(map.get("aloneProject"))));
        job.setAddress(String.valueOf(map.get("jobAddr")));
        job.setSalary(new BigDecimal(String.valueOf(map.get("jobSal"))));
        job.setRequirement(String.valueOf(map.get("requirement")));
        job.setSex(String.valueOf(map.get("sex")));
        job.setWorkTime(String.valueOf(map.get("workTime")));

        return ResultUtil.success("更新成功!");
    }


}

